import os
import sys

import logzero
import stackprinter
from fbs_runtime.application_context.PyQt5 import ApplicationContext

from qt_frame import SPAC800

try:
    logfile_name = "SPAC800_run.log"
    logfile_path = os.path.join("./", logfile_name)
    logger = logzero.setup_logger("SPAC800", logfile=logfile_path, maxBytes=10 * 2 ** 20, backupCount=10)
except Exception:
    logger = logzero.logger


class AppContext(ApplicationContext):  # 1. Subclass ApplicationContext
    def run(self):  # 2. Implement run()
        _ = SPAC800(app=self.app, logger=logger)
        return self.app.exec_()  # 3. End run() with this line


if __name__ == "__main__":
    try:
        appctxt = AppContext()  # 4. Instantiate the subclass
        exit_code = appctxt.run()  # 5. Invoke run()
        sys.exit(exit_code)
    except Exception:
        logger.error("in main crash {}".format(stackprinter.format()))
